package at.fhj.itm11.forum.beans;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;

import at.fhj.itm11.forum.data.CommentRepository;
import at.fhj.itm11.forum.model.Comment;
import at.fhj.itm11.forum.model.Post;
import at.fhj.itm11.forum.model.User;
import at.fhj.itm11.forum.util.ForumConstants;

@ManagedBean
@RequestScoped
public class CommentsBean implements Serializable {

	private static final long serialVersionUID = -5203189585531897908L;

	@Inject
	private CommentRepository commentRepository;

	@Inject
	private AuthenticationBean authBean;

	private User user;
	private Post post;

	public List<Comment> loadComments(Post post) {
		this.setPost(post);
		user = (User) authBean.getSession().getAttribute(
				ForumConstants.SESSION_CURRENT_USER);
		return commentRepository.findCommentsByPostOrderedByDate(post.getId());
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
	}

}
